home *** CD-ROM | disk | FTP | other *** search
- Path: news.dx.net!NewsWatcher!user
- From: mikedorman@cedarnet.com (Mike Dorman)
- Newsgroups: comp.lang.c
- Subject: Re: Problem with c code, please help!
- Date: Fri, 19 Jan 1996 07:25:37 -0600
- Organization: CedarNet Online
- Message-ID: <mikedorman-1901960725380001@205.148.200.150>
- References: <surgsw-1901960148530001@128.206.206.86>
- NNTP-Posting-Host: 205.148.200.150
-
- In article <surgsw-1901960148530001@128.206.206.86>,
- surgsw@mizzou1.missouri.edu (Joel Weinstein) wrote:
-
- > I have been trying to get this very simple piece of code to work for
- > hours. What is the problem???????
- >
- > #include <stdio.h>
- >
- > main()
- > {
- > int i=0;
- > char word[100], c;
-
- word is an array of 100 chars, there are no pointers declared here.
- change word to this:
- char *word;
- and it will be *much* easier.
-
- > printf("Enter a word: ");
- > while( (c = getchar()) != '\n') {
- > *word = c;
- here, replace *word (which is a pointer to something...you didn't specify
- which char in word you wanted it to point to, so it could be pointing to
- just about anything) with
- strcat(word, *c);
- which will copy c onto the end of word, adding the character to word.
- > word == word + 1;
- you don't need this...just get rid of it.
-
- > }
- > printf("You entered: ");
- > *word = 0;
- > while( *word != '\n' )
- > {
- > putchar( *word );
- > word == word + 1;
- > }
- Just get rid of that whole while loop, and replace it with:
- puts(word);
-
- > }
- >
- > I think the problem I am having is due to my not knowing when to use the
- > '*' operator. When do you use it? Also, why won't my goddam compiler let
-
- Ok, one problem at a time...
-
- When you say "char word[100]", that defines a object (and object it
- anything in memory) called word, that contains 100 chars (or 100 byte
- values). So, later when word is referenced (like any array) you need to
- tell the compiler which value in the array you want by adding brackets to
- the end (so, if you wanted the first char in word, you'd say word[0])
- (arrays are referenced from *ZERO* so array[0] is really the first value
- in the array).
-
- When you use the * operator, that returns a pointer to something. So
- *word returns a pointer to word, an array of chars. I'm not sure how
- standard C deals with this, but i would recommend not doing it that way.
- If you needed a pointer to a string from a char array (which is kindof
- what you need, sortof) you'd get one by getting a pointer to the first
- char (*word[0]).
-
- Also, when getting string information from the user, you probably
- shouldn't use a char array (I know it seems to make more sense that way,
- but soon it'll become clear that it doesn't), but you should use a string
- pointer, like this:
- char *word;
- Now, word is a pointer to a string, which has no specific or limited
- length. Then you can use the standard functions like strcpy and strcat to
- work on string pointers.
-
- To do it with an array, you need another variable to keep track of how
- many characters you've already put into word, so you can index the array
- correctly:
-
-
-
- main()
- {
- char word[100], c;
- int i;
- i = 0;
-
- puts("Enter a word:");
- while((c = getch()) != \n)
- {
- word[i] = c;
- i++;
- }
-
- puts("You entered: ");
- puts(*word[0]);
- }
-
- But, if you did it with a string pointer, it's this much easier:
-
- main()
- {
- char *word;
-
- puts("Enter a word: ");
- while(scanf("%s", word) != 0)
-
- puts("You entered: ");
- puts(word);
- }
-
-
- > me do word++; instead of word == word +1; I also tried to do *word++;
- > and it didn't work, what is the deal with that. It won't let me put word
- > = word + 1;. It insists on the double ='s. Why is that?
-
- Are you *SURE*?? There is a difference between = and == in C. = is
- actually the assignment operator (ie: in a = b, a is assigned the value of
- b). == is the equality test operator (ie: 1 == 1 returns TRUE (1) ). So,
- word == word + 1 would alwas return false. You should be able to do
- word++ (although probably the reason why you couldn't here was because
- word was an array of chars and the compiler didn't know what to do with
- word, it would've been ok if you did word[0]++, but you wouldn't have
- needed it anyway.
-
- > ps: is there a way to find out what exactly error messages mean? I kept
- > getting something similar to: not an Ivalue. It would be nice if I knew
- > what the hell that meant.
-
- An lvalue is a value that can be assigned to. For instance, if you had 3
- = b + 2, the compiler would complain about the 3 because you can't store
- another value to 3. It probably complained about word = word + 1 because
- word is a "constant" that defines the beggining of the char array. I
- don't really know what you get from "word" if you have a "char word[100]"
- array, except something really wierd, so you probably shouldn't use it.
-
- Well, there it is! Hope that helps! Feel free to email me if you have
- other problems.
-
- ==============================================
- From: Mike Dorman
- mikedorman@cedarnet.com
- 1:283/119.1012@fidonet
- http://www.cedarnet.com/bbs/users/mikedorman/
- ==============================================
-